import separateHeader from '@/components/separate-header.vue';

import { fetchShopList } from '@/api/shop.js';

export default {
	components: {
		separateHeader
	},
	data() {
		return {
			shopList: [],
			pageParam: {
				pageNum: 0,
				pageSize: 8
			},
			loadingType: 'more', //加载更多状态
		};
	},

	onLoad(options) {
		this.loadData();
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	//加载更多
	onReachBottom() {
		this.pageParam.pageNum++;
		this.loadData();
	},
	methods: {
		//加载商品 ，带下拉刷新和上滑加载
		async loadData(type = 'add', loading) {
			//没有更多直接返回
			if (type === 'add') {
				if (this.loadingType === 'nomore') {
					return;
				}
				this.loadingType = 'loading';
			} else {
				this.loadingType = 'more'
			}

			if (type === 'refresh') {
				this.pageParam.pageNum=0;
				this.shopList = [];
			}
			fetchShopList(this.pageParam).then(response => {
				let pageData = response.data;
				//在原先list中追加数据
				this.shopList = this.shopList.concat(pageData.content);
				
				if(pageData.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.loadingType = 'more';
				}
				if (type === 'refresh') {
					if (loading == 1) {
						uni.hideLoading()
					} else {
						uni.stopPullDownRefresh();
					}
				}
			});
		},
		
		//详情
		navToShopDetailPage(item) {
			uni.navigateTo({
				url: `/pages/shop/shop?id=${item.id}`
			})
		},
		stopPrevent() {}
	},
}